extends Node

@onready var item_pool: ItemPool = preload("res://data/item_pool/item_pool.tres")
## 合成表
@onready var recipe_book: RecipeBook = RecipeBook.new()

## 获取预览装备的描述
func get_item_info(item: PandoraEntity, color: String = "grey"):
	var des := ""
	var level = item.get_integer("level")
	if level == 1:
		des += "[color=grey]品质:普通[/color]"
		color = "grey"
	elif level == 2:
		des += "[color=#3F704D]品质:稀有[/color]"
		color = "#3F704D"
	elif level == 3:
		des += "[color=purple]品质:史诗[/color]"
		color = "purple"
	elif level == 4:
		des += "[color=orange]品质:传说[/color]"
		color = "orange"
		
	var base_damage = item.get_float("base_damage")
	if base_damage > 0:
		des += "\n[color=" + color +"]攻击力+" + str(base_damage) +"[/color]"
	
	var move_speed = item.get_integer("move_speed")
	if move_speed > 0:
		des += "\n[color=" + color +"]移速+" + str(move_speed) +"[/color]"

	var attack_speed = item.get_integer("attack_speed")
	if attack_speed > 0:
		des += "\n[color=" + color +"]攻击速度+" + str(attack_speed) +"[/color]"
	## 减少显示
	if attack_speed < 0:
		des += "\n[color=black]攻击速度" + str(attack_speed) +"[/color]"
		
	var critical_chance = item.get_integer("critical_chance")
	if critical_chance > 0:
		des += "\n[color=" + color +"]暴击率+" + str(critical_chance) +"%[/color]"
		
	var hp = item.get_integer("hp")
	if hp > 0:
		des += "\n[color=" + color +"]血量+" + str(hp) +"[/color]"
		
	var mp = item.get_integer("mp")
	if mp > 0:
		des += "\n[color=" + color +"]蓝量+" + str(mp) +"[/color]"	
		
	var health_regen = item.get_float("health_regen")
	if health_regen > 0:
		des += "\n[color=" + color +"]血量恢复+" + str(health_regen) +"[/color]"	
		
	var mp_regen = item.get_float("mp_regen")
	if mp_regen > 0:
		des += "\n[color=" + color +"]蓝量恢复+" + str(mp_regen) +"[/color]"
		
	var exp_bonus = item.get_float("exp_bonus")
	if exp_bonus > 0:
		des += "\n[color=" + color +"]经验加成+" + str(roundi(exp_bonus * 100)) +"%[/color]"	
		
	var cooldown_reduction = item.get_float("cooldown_reduction")
	if cooldown_reduction > 0:
		des += "\n[color=" + color +"]冷却缩减+" + str(roundi(cooldown_reduction * 100)) +"%[/color]"		
			
	var add_attack_probability = item.get_float("add_attack_probability")
	if add_attack_probability > 0:
		des += "\n[color=" + color +"]攻击" + str(roundi(add_attack_probability * 100)) +"%概率增加0.1攻击力[/color]"
		
	var add_coin_probability = item.get_float("add_coin_probability")
	if add_coin_probability > 0:
		des += "\n[color=" + color +"]攻击" + str(roundi(add_coin_probability * 100)) +"%概率增加1金币[/color]"	

	var add_mp_probability = item.get_float("add_mp_probability")
	if add_mp_probability > 0:
		des += "\n[color=" + color +"]攻击" + str(roundi(add_mp_probability * 100)) +"%概率增加0.1蓝量[/color]"	

	var add_hp_probability = item.get_float("add_hp_probability")
	if add_hp_probability > 0:
		des += "\n[color=" + color +"]攻击" + str(roundi(add_hp_probability * 100)) +"%概率增加0.1血量[/color]"	

	var add_exp_probability = item.get_float("add_exp_probability")
	if add_exp_probability > 0:
		des += "\n[color=" + color +"]攻击" + str(roundi(add_exp_probability * 100)) +"%概率增加1经验[/color]"	
	
	
	var description = item.get_string("description")	
	description = description.replace("\\n", "\n")
	if !description.is_empty():
		des += "\n[font_size=14][color=red]" + str(description) +"[/color][/font_size]"	
	
	
	var array = recipe_book.get_sub_recipe(item.get_entity_id())
	if !array.is_empty():
		for sub_array in array:
			des += "\n[font_size=14][color=#555543][%s->%s][/color][/font_size]" % [sub_array[0], sub_array[1]]

	return des	

## 获取装备名字
func get_item_name(id) -> String:
	return Pandora.get_entity(id).get_entity_name()

## 专门添加四级装备特效
func add_item_buff(item: PandoraEntity):
	match item.get_entity_name():
		"永恒之叶":
			## 获取4%的最大生命值生命恢复 获取4%的最大蓝量蓝量恢复
			var regen_health_buff = BuffFactory.create_heal_buff_percent(4, 1, -1)
			Utils.player.buff_manager.add_buff(regen_health_buff, Utils.player)
			var regen_mana_buff = BuffFactory.create_mana_buff_percent(4, 1, -1)
			Utils.player.buff_manager.add_buff(regen_mana_buff, Utils.player)
		"星速":
			var star_speed_buff = BuffFactory.create_custom_buff("星速", -1)
			Utils.player.buff_manager.add_buff(star_speed_buff, Utils.player)
		"燃烧的蜡烛":
			var star_speed_buff = BuffFactory.create_custom_buff("燃烧的蜡烛", -1)
			Utils.player.buff_manager.add_buff(star_speed_buff, Utils.player)
		"幸运金币":
			var star_speed_buff = BuffFactory.create_custom_buff("幸运金币", -1)
			Utils.player.buff_manager.add_buff(star_speed_buff, Utils.player)
		"终极之书":
			var final_book = BuffFactory.create_custom_buff("终极之书", -1)
			Utils.player.buff_manager.add_buff(final_book, Utils.player)
		"闪亮之珠":
			var shining = BuffFactory.create_custom_buff("闪亮之珠", -1)
			Utils.player.buff_manager.add_buff(shining, Utils.player)
		"涌动戒指":
			var surging_ring = BuffFactory.create_custom_buff("涌动戒指", -1)
			Utils.set_player_stats_value("is_skill_critical", true)
			Utils.player.buff_manager.add_buff(surging_ring, Utils.player)
		"战士精神":
			var warrior_spirit = BuffFactory.create_custom_buff("战士精神", -1)
			Utils.player.weapon.scale *= 2
			Utils.player.buff_manager.add_buff(warrior_spirit, Utils.player)
		"曲奇饼干":
			var cookie = BuffFactory.create_custom_buff("曲奇饼干", -1)
			Utils.player.buff_manager.add_buff(cookie, Utils.player)
		"死神之书":
			var die = BuffFactory.create_custom_buff("死神之书", -1)
			Utils.player.buff_manager.add_buff(die, Utils.player)
## 专门删除四级装备特效
func remove_item_buff(item: PandoraEntity):
	match item.get_entity_name():
		"永恒之叶":
			var regen_health_buff = BuffFactory.create_heal_buff_percent(4, 1, -1)
			Utils.player.buff_manager.remove_buff(regen_health_buff.get_buff_name())
			var regen_mana_buff = BuffFactory.create_mana_buff_percent(4, 1, -1)
			Utils.player.buff_manager.remove_buff(regen_mana_buff.get_buff_name())
		"星速":
			var star_speed_buff = BuffFactory.create_custom_buff("星速", -1)
			Utils.player.buff_manager.remove_buff(star_speed_buff.get_buff_name())
		"燃烧的蜡烛":
			var candle_buff = BuffFactory.create_custom_buff("燃烧的蜡烛", -1)
			Utils.player.buff_manager.remove_buff(candle_buff.get_buff_name())	
		"幸运金币":
			var lucky_coin = BuffFactory.create_custom_buff("幸运金币", -1)
			Utils.player.buff_manager.remove_buff(lucky_coin.get_buff_name())
		"终极之书":
			var final_book = BuffFactory.create_custom_buff("终极之书", -1)
			Utils.player.buff_manager.remove_buff(final_book.get_buff_name())
		"闪亮之珠":
			var shining = BuffFactory.create_custom_buff("闪亮之珠", -1)
			Utils.player.buff_manager.remove_buff(shining.get_buff_name())
		"涌动戒指":
			var surging_ring = BuffFactory.create_custom_buff("涌动戒指", -1)
			Utils.set_player_stats_value("is_skill_critical", false)
			Utils.player.buff_manager.remove_buff(surging_ring.get_buff_name())
		"战士精神":
			var warrior_spirit = BuffFactory.create_custom_buff("战士精神", -1)
			Utils.player.weapon.scale = Vector2.ONE
			Utils.player.buff_manager.remove_buff(warrior_spirit.get_buff_name())
		"曲奇饼干":
			var cookie = BuffFactory.create_custom_buff("曲奇饼干", -1)
			Utils.player.buff_manager.remove_buff(cookie.get_buff_name())
		"死神之书":
			var die = BuffFactory.create_custom_buff("死神之书", -1)
			Utils.player.buff_manager.remove_buff(die.get_buff_name())
